package com.hutianao.gqgl.login.control;

import com.alibaba.fastjson.JSON;
import com.hutianao.gqgl.login.entity.MenuLevelEntity;
import com.hutianao.gqgl.mybatis.entity.PtMenuEntity;
import com.hutianao.gqgl.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hutianao.gqgl.user.entity.SecurityUser;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@Controller
public class MainController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private UserService userService;

    @RequestMapping("/")
    public String main(HttpServletRequest request){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof SecurityUser){
            SecurityUser securityUser = (SecurityUser) principal;
            String userUuid = securityUser.getUserUuid();
            List<MenuLevelEntity> list = userService.queryMenuLevelList(userUuid);
            request.getSession().setAttribute("menu",list);
        }else{
            List<MenuLevelEntity> list = userService.queryMenuLevelList();
            request.getSession().setAttribute("menu",list);
        }

        logger.info("main");
        return "sm/main";
    }
    @RequestMapping("/toChange")
    public String toForget(HttpServletRequest request){
        Locale locale =LocaleContextHolder.getLocale();
        request.getSession().setAttribute("siteLanguage",locale.toString());
        logger.info("toChange");
        return "password/Change";
    }

    @RequestMapping("/index")
    public String index(){
        return "login/index";
    }

    @RequestMapping("/temp")
    public String temp(){
        return "login/temp";
    }

    @RequestMapping("/PasswordUpdate")
    public String PasswordUpdate(){
        return "login/PasswordUpdate";
    }

    @RequestMapping("/queryMenuList")
    @ResponseBody
    public List<PtMenuEntity> queryMenuList(){
        List<PtMenuEntity> list = userService.queryMenuList();
        return  list;
    }

    @RequestMapping("/queryMenuLevelList")
    @ResponseBody
    public String queryMenuLevelList(){
        List<MenuLevelEntity> list = userService.queryMenuLevelList();
        return JSON.toJSONString(list);
    }
    @RequestMapping("/loadServiceMenu")
    @ResponseBody
    public List<MenuLevelEntity> loadServiceMenu(@RequestParam("menuId") String menuId,HttpServletRequest request){
        List<MenuLevelEntity> list = (List<MenuLevelEntity>) request.getSession().getAttribute("menu");
        for(MenuLevelEntity item: list){
            if(menuId.equals(item.getMenuId())){
                return item.getChildren();
            }
        }
        return null;
    }

    @GetMapping("/403")
    public String show403() {
        return "error/403";
    }


}
